﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PoolService.cs" company="">
//   
// </copyright>
// <summary>
//   The pool service.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace F5.BigIp.LocalTrafficManager.Proxy.V11
{
    using System.Collections.Generic;

    using F5.BigIp.LocalTrafficManager.Interfaces;
    using F5.BigIp.LocalTrafficManager.Proxy.V11.LocalLB.Pool;

    /// <summary>The pool service.</summary>
    public class PoolService : IPoolService
    {
        #region Public Methods and Operators

        /// <summary>The get pools.</summary>
        /// <param name="loadBalancer">The load balancer.</param>
        /// <returns>The <see cref="IEnumerable"/>.</returns>
        public IEnumerable<IPool> GetPools(LoadBalancer loadBalancer)
        {
            LocalLBPoolPortType poolService = ServiceFactory.CreatePoolService(loadBalancer);

            get_listResponse poolListResponse = poolService.get_list(new get_listRequest());

            string[] poolNames = poolListResponse.@return;

            get_object_statusResponse poolStatusResponse =
                poolService.get_object_status(new get_object_statusRequest(poolNames));

            var pools = new List<IPool>();

            if (poolNames != null && poolNames.Length > 0)
            {
                for (int i = 0; i < poolNames.Length; i++)
                {
                    var pool = new Pool(poolNames[i], loadBalancer);

                    LocalLBObjectStatus status = poolStatusResponse.@return[i];

                    if (status.enabled_status == LocalLBEnabledStatus.ENABLED_STATUS_ENABLED
                        && status.availability_status == LocalLBAvailabilityStatus.AVAILABILITY_STATUS_GREEN)
                    {
                        pool.Status = PoolStatus.Enabled;
                    }

                    pools.Add(pool);
                }
            }

            return pools;
        }

        #endregion
    }
}